﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem043
    {
        /// <summary>
        /// Find the sum of all pandigital numbers with an unusual sub-string divisibility property.
        /// </summary>
        [EulerProblem(43, Title = "Find the sum of all pandigital numbers with an unusual sub-string divisibility property.")]
        public static long Solve()
        {
            long[] digits = new long[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            return (long)
                digits.Permute()
                    //.Where(perm => perm.First() != 0)
                    .Select(perm => new 
                        {
                            number = perm.Reverse().ToInt64(),
                            d2d3d4 = perm.Skip(1).Take(3).Reverse().ToInt64(),
                            d3d4d5 = perm.Skip(2).Take(3).Reverse().ToInt64(),
                            d4d5d6 = perm.Skip(3).Take(3).Reverse().ToInt64(),
                            d5d6d7 = perm.Skip(4).Take(3).Reverse().ToInt64(),
                            d6d7d8 = perm.Skip(5).Take(3).Reverse().ToInt64(),
                            d7d8d9 = perm.Skip(6).Take(3).Reverse().ToInt64(),
                            d8d9d10 = perm.Skip(7).Take(3).Reverse().ToInt64()
                        })
                    .Where(pandigital =>
                        pandigital.d2d3d4 % 2 == 0 &&
                        pandigital.d3d4d5 % 3 == 0 &&
                        pandigital.d4d5d6 % 5 == 0 &&
                        pandigital.d5d6d7 % 7 == 0 &&
                        pandigital.d6d7d8 % 11 == 0 &&
                        pandigital.d7d8d9 % 13 == 0 &&
                        pandigital.d8d9d10 % 17 == 0 )
                    .Select(pandigital => pandigital.number)
                    .Sum();
        }
    }
}
